package com.xu.cloud.common.exception;

import com.xu.cloud.common.enums.ExceptionEnum;
import lombok.Getter;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xuguan
 * @since 2023/7/13
 */
@Getter
public enum ErrorCodeEnum implements ExceptionEnum<ErrorCodeEnum> {
    // SUCCESS
    OK("200", "成功"),
    // Bad Request
    BAD_REQUEST("400", "参数错误"),
    // UnAuthenticated
    UNAUTHENTICATED("401", "未认证"),
    // UnAuthorized
    UNAUTHORIZED("403", "未授权"),
    // Not Found
    NOT_FOUND("404", "请求不存在"),
    // Internal Server Error
    INTERNAL_SERVER_ERROR("500", "内部异常"),

    ERR_LACK_PARAM("1000", "参数缺失"),
    ERR_ILLEGAL_PARAM("1001", "参数不合法"),
    ERR_RATE_LIMIT("1002", "请求频率太快, 请稍后再试"),
    ERR_REQUEST_ILLEGAL("1003", "请求非法"),
    ERR_REQUEST_REPEAT("1004", "重复请求"),
    ;

    private final String code;
    private final String message;
    private static final Map<String, ErrorCodeEnum> ERROR_CODE_ENUM_MAP = new HashMap<>();

    static {
        Arrays.stream(values())
                .forEach(errorInfo -> ERROR_CODE_ENUM_MAP.put(errorInfo.getCode(), errorInfo));
    }

    ErrorCodeEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ErrorCodeEnum getErrorCodeEnumByCode(String code) {
        return ERROR_CODE_ENUM_MAP.get(code);
    }

    public ErrorCodeInfo info() {
        return new ErrorCodeInfo(code, message);
    }
}
